xquery version "1.0";
module namespace ddi-exist="http://code.google.com/p/ddi-exist/search";

(:ddi namespaces:)
declare namespace g = "ddi:group:3_1";
declare namespace dc="ddi:dcelements:3_1";
declare namespace d="ddi:datacollection:3_1"; 
declare namespace dc2="http://purl.org/dc/elements/1.1/"; 
declare namespace s="ddi:studyunit:3_1"; 
declare namespace c="ddi:conceptualcomponent:3_1";
declare namespace r="ddi:reusable:3_1"; 
declare namespace a="ddi:archive:3_1"; 
declare namespace ddi="ddi:instance:3_1"; 
declare namespace l="ddi:logicalproduct:3_1";

(:filter a collection of studies based on series:)
declare function ddi-exist:filterSeries($collection as node()*, $series as xs:string) as node()*
{
    $collection/ddi:DDIInstance/s:StudyUnit[contains(concat(',', $series, ','), concat(',', r:UserID[@type='series_id'], ','))]
};

(: find studies :)
declare function ddi-exist:searchStudy($search as xs:string, $lang as xs:string, $collection as node()*) as node()*
{
    let $studies :=
        if(empty($search))
        then
            ()
        else    
            if(empty($lang))
                then
                    $collection//s:StudyUnit[.//r:Title[ft:query(., $search)] or 
                    .//r:Content[ft:query(., $search)] or 
                    .//r:Creator[ft:query(., $search)] or
                    .//r:Keyword[ft:query(., $search)]]
                else
                    $collection//s:StudyUnit[.//r:Title[ft:query(., $search)] or 
                    .//r:Content[ft:query(., $search)] or 
                    .//r:Creator[ft:query(., $search)] or
                    .//r:Keyword[ft:query(., $search)]]
    
    (:skip unnecesary elements :)
    return 
        for $s in $studies
            return
            <StudyUnit>
                <url>http://xml.snd.gu.se/{util:document-name($s)}</url>
                <id>{fn:string($s/@id)}</id>
                <title>
                    {for $t in $s/r:Citation/r:Title
                        return
                            element {$t/@xml:lang} {fn:string($t)}
                    }
                </title>
                <abstract>
                    {for $a in $s//s:Abstract/r:Content
                        return
                            element {$a/@xml:lang} {fn:string($a)}
                    }                    
                </abstract>
                <creator>
                    {for $a in $s/r:Citation/r:Creator
                        return
                            element {$a/@xml:lang} {<name>{fn:string($a)}</name>,<affiliation>{fn:string($a/@affiliation)}</affiliation>}
                    }                    
                </creator>                
                <questions>{count($s//d:QuestionText/..)}</questions>
                <variables>{count($s//l:Variable)}</variables>
            </StudyUnit>
};

(: find questions :)
declare function ddi-exist:searchQuestion($search as xs:string, $lang as xs:string, $collection as node()*) as node()*
{
    if(empty($search))
    then
        ()
    else
        if(empty($lang))
        	then
        		$collection//.[ft:query(d:QuestionText, $search)]
        	else
        		$collection//.[ft:query(d:QuestionText[@xml:lang=$lang], $search)]
};

(: find variables :)
declare function ddi-exist:searchVariable($search as xs:string, $lang as xs:string, $collection as node()*) as node()*
{
    if(empty($search))
    then
        ()
    else
        if(empty($lang))
            then
        		$collection//l:Variable[
                                .//r:Label[ft:query(., $search)] (:or 
                                ./l:ConceptReference/r:ID = ddi-exist:searchConcept($search, $lang, $collection)/@id :)
                                ]
        	else
        		$collection//l:Variable[.//r:Label[ft:query(., $search)]]
};

(: find variables :)
declare function ddi-exist:searchConcept($search as xs:string, $lang as xs:string, $collection as node()*) as node()*
{
    if(empty($search))
    then
        ()
    else
        if(empty($lang))
            then
            	$collection//l:Concept[ft:query(., $search)]
        	else
        		$collection//l:Concept[ft:query(., $search)]
};

declare function ddi-exist:limitMatches($nodes as node()*, $start as xs:integer, $records as xs:integer) as node()*
{
    (: compute the limits for this page :)    
    let $max := count($nodes)

    let $end :=  min (($start + $records ,$max))
     
    (: restrict the full set of matches to this subsequence :)
    return subsequence($nodes,$start,($end+1))    
};

declare function ddi-exist:getQuestions($questionId as xs:integer, $collection as node()*) as node()*
{
    $collection//.[r:UserID[@type='question_id'] = $id]/ancestor::d:MultipleQuestionItem  
};
